import flask

def sqlUpdate(db,dbDatabase,tableName,condition,verify):
    cursor = db.cursor()
    #表结构
    cursor.execute('select COLUMN_NAME from information_schema.columns where TABLE_SCHEMA="' + dbDatabase + '" and TABLE_NAME="'+ tableName +'";')
    result = cursor.fetchall()
    #'username','passwd'
    header = str(result).replace(",","").replace("(","").replace(")","").replace(" ",",")
    print("derBug 原表头 : " + header)

    #查询要修改的数据
    #derBug
    print("derBug SQL Statement: " + 'select * ' + 'from ' + tableName + ' where ' + condition + ';')
    cursor.execute('select * ' + 'from ' + tableName + ' where ' + condition + ';')
    #result = cursor.fetchall()
    #body = str(result).replace(",","").replace("(","").replace(")","").replace(" ",",")
    body = cursor.fetchall()

    print("derBug condition : "+condition)

    # SQL set 值 passwd="000000",username="test"
    setValue = flask.request.values.get('setValue')
    #将set语句转换为查询时的as语句 排除要修改的列 模拟修改结果
    newSetValue = setValue.replace("="," as ").split(",")
    i = 0
    newSetValueLast = ""
    newHeader = header
    print("derBug Len : " + len(newSetValue).__str__())
    #将set后from前的内容使用” as “分割后再前后调换
    while i < len(newSetValue):
        print("derBug newSetVlaue : " + newSetValue[i])
        setOne = newSetValue[i].split(" as ")
        newSetValueLast = setOne[1] + " as " + setOne[0]

        print("derBug While setOne 0 : " + setOne[0])
        print("derBug While newSetValueLast : " + newSetValueLast)
        print("derBug While newHeader 0 : " + newHeader)

        newHeader = newHeader.replace(setOne[0],newSetValueLast)
        print("derBug While newHeader 1 : " + newHeader)
        i = i + 1

    asSQL = newSetValueLast.strip(",")

    print("derBug LAST VALUE : " + asSQL)
    print("derBug updateSQLSimulate : " + 'select ' + newHeader.replace("'","") + ' from ' + tableName + ' where ' + condition + ' union select * from ' + tableName + ' where ' + condition.replace("=","!=") + ';')

    #BUG 可能会模拟出非法的修改结果
    cursor.execute('select ' + newHeader.replace("'","") + ' from ' + tableName + ' where ' + condition + ' union select * from ' + tableName + ' where ' + condition.replace("=","!=") + ';')
    updataYL = cursor.fetchall()

    if verify == 'ok':
        #执行修改
        cursor.execute('update ' + tableName + ' set ' + setValue + ' where ' + condition +';')
        cursor.fetchone()
        #提交数据
        db.commit()
        db.close()
    ren = {'msg':'Update Row','header':header,'body':body,'table':updataYL,'msg_code':200}
    return ren